﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cn.Util;

/// <summary>
/// String扩展
/// </summary>
public static class StringExt
{
    /// <summary>
    /// 包含查询
    /// </summary>
    /// <param name="str"></param>
    /// <param name="likeStr"></param>
    /// <returns></returns>
    public static bool Like(this string str, string likeStr)
    {
        return str.IndexOf(likeStr) != -1;
    }

    /// <summary>
    /// 普通MD5加密
    /// </summary>
    public static string ExtMD5(this string str)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str.ToString(), "MD5").ToLower();
    }

    /// <summary>
    /// 生成Guid
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ExtNewGuid(this string str)
    {
        return Guid.NewGuid().ToString();
    }

    /// <summary>
    /// Base64解码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ExtBase64Decoding(this string str)
    {
        return SBase64.DecodingForString(str);
    }

    /// <summary>
    /// Base64编码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ExtBase64Encoding(this string str)
    {
        return SBase64.EncodingForString(str);
    }

    /// <summary>
    /// 过滤非法字符
    /// </summary>
    /// <param name="str">要处理的字符</param>
    /// <returns>string</returns>
    public static string ExtFilter(this string str)
    {
        if (!string.IsNullOrEmpty(str))
        {
            str = str.ToLower();
            str = str.Replace("<", " >");//处理小于号   
            str = str.Replace(">", "< ");//处理大于号  
            str = str.Replace("&", "");
            str = str.Replace("%20", "");
            str = str.Replace("--", "");
            str = str.Replace("==", "");
            str = str.Replace("%", "");
        }
        return str;
    }


    /// <summary>
    /// 截取字符串某一长度并返回
    /// </summary>
    /// <param name="p_StrValue">要截取的字符串</param>
    /// <param name="p_StrLen">要截取的字符串长度</param>
    /// <param name="p_HavePoints">截取长度小于总长度时是否要加省略号</param>
    /// <returns>截取后的字符串</returns>
    public static string ExtLeft(this string p_StrValue, int p_StrLen, bool p_HavePoints = true)
    {
        string m_NewStr = "";
        int m_Num = 0;
        for (int i = 0; i < p_StrValue.Length; i++)
        {
            char c = Convert.ToChar(p_StrValue.Substring(i, 1));
            if (((int)c > 255) || ((int)c < 0)) { m_Num = m_Num + 2; }
            else { m_Num = m_Num + 1; }
            if (m_Num >= p_StrLen)
            {
                m_NewStr = p_StrValue.Substring(0, i + 1);
                if (p_HavePoints == true && i < p_StrValue.Length)
                { m_NewStr = m_NewStr + "..."; }
                break;
            }
            else
            {
                m_NewStr = p_StrValue;
            }
        }
        return m_NewStr;
    }


    /// <summary>
    /// 将数据库的表名转换为实体类对应的名字
    /// </summary>
    /// <param name="str">要处理的字符</param>
    /// <returns>string</returns>
    public static string ExtToModelName(this string str)
    {
        return str.Replace("_", "");
    }

}

